package com.teckteam.ptvsystem.view;

import java.awt.Adjustable;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.teckteam.ptvsystem.controller.CancelWindowListener;

/**
 * Timetable display window
 * @param <T>
 *
 */
public class TimetableDialog extends JDialog
{
   /**
    * Debug constructor using scraped data (always works)
    * @param times
    */
   public TimetableDialog(LinkedHashMap<String, String[]> times)
   {
      int i;
      ArrayList<String> nameDestination = new ArrayList<String>(),
                        nameKey = new ArrayList<String>();
      Iterator<String> iterator = times.keySet().iterator();
      JButton closeButton = new JButton("Close");
      JPanel btnPanel = new JPanel();
      
      setModal(true);
      btnPanel.add(closeButton);
      setLayout(new BorderLayout());
      
      closeButton.addActionListener(new CancelWindowListener(this));
      
      while (iterator.hasNext())
      {
         String s = iterator.next();
         if (s.compareTo("routeHeader") == 0 || s.compareTo("ampmCols") == 0)
            continue;
         
         nameKey.add(s);
         nameDestination.add(s.split("[|]")[0]);
      }
      
      String timetableArr[][] = new String[nameKey.size()][];
      for (i = 0; i < nameDestination.size(); i++)
         timetableArr[i] = times.get(nameKey.get(i));
      
      String ampmArr[] = times.get("ampmCols");
      
      
      DefaultTableModel tableModel = new DefaultTableModel(timetableArr,
            times.get("ampmCols"));
      DefaultTableModel stationColumn = new DefaultTableModel();
      stationColumn.addColumn("Station Names", nameDestination.toArray());
      nameKey.removeAll(nameKey);
      
      //Make cells non-editable
      JTable rowTable = new JTable(stationColumn)
      {
         public boolean isCellEditable(int r, int c)
         {
            return false;
         }
      },
             
      timeTable = new JTable(tableModel)
      {
         public boolean isCellEditable(int r, int c)
         {
            return false;
         }
      };
      
      JPanel timeTablePanel = new JPanel(new BorderLayout());
      Dimension rowTableDim = new Dimension(260, 5000);
      
      timeTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      rowTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      rowTable.setPreferredSize(rowTableDim);
      
      final JScrollPane timeInfoScroll = new JScrollPane(timeTable);
      final JScrollPane rowScroll = new JScrollPane(rowTable);
      rowScroll.setPreferredSize(rowTableDim);
      
      //Logic to affix row labels in view whilst having times scrollable.
      rowScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
      rowScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
      timeInfoScroll.getVerticalScrollBar().addAdjustmentListener(
            new AdjustmentListener()
      {
         public void adjustmentValueChanged(AdjustmentEvent e)
         {
            Adjustable source = e.getAdjustable();
            if (e.getValueIsAdjusting())
               rowScroll.getVerticalScrollBar().setValue(e.getValue());
         }
      });
      
      timeInfoScroll.addMouseWheelListener(new MouseWheelListener()
      {
         public void mouseWheelMoved(MouseWheelEvent e)
         {
            rowScroll.getVerticalScrollBar().setValue(timeInfoScroll.getVerticalScrollBar().getValue());
         }
      });
      
      timeInfoScroll.setPreferredSize(new Dimension(600, 700));
      // rowTable.setPreferredSize(new Dimension(80, 800));
      timeTablePanel.add(rowScroll, BorderLayout.WEST);
      timeTablePanel.add(timeInfoScroll, BorderLayout.CENTER);
      
      this.setTitle(times.get("routeHeader")[0]);
      this.setSize(960, 700);
      this.setLocationRelativeTo(null);
      
      this.add(timeTablePanel, BorderLayout.CENTER);
      this.add(btnPanel, BorderLayout.SOUTH);
      this.setVisible(true);
   }
}
